package cn.it.shop.wechat.dispatcher;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.util.SystemPropertyUtils;

import cn.it.shop.util.HttpPostUploadUtil;
import cn.it.shop.util.MessageUtil;
import cn.it.shop.wechat.response.Image;
import cn.it.shop.wechat.response.ImageMessage;
import net.sf.json.JSONObject;

/**
 * ClassName: EventDispatcher
 * @Description: 事件消息业务分发器
 * @author fangyangsheng
 */
@Component("eventDispatcher")
public class EventDispatcher {
	
    public String processEvent(Map<String, String> map) {
        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { //关注事件
            System.out.println("==============这是关注事件！");
            
            String openid = map.get("FromUserName"); // 用户 openid
            String mpid = map.get("ToUserName"); // 公众号原始 ID
            //响应类型为图片信息
            ImageMessage imgmsg = new ImageMessage();
            imgmsg.setToUserName(openid);
            imgmsg.setFromUserName(mpid);
            imgmsg.setCreateTime(new Date().getTime());
            imgmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_Image);
            
            Image img = new Image();
            HttpPostUploadUtil  util = new HttpPostUploadUtil();
            String filepath="H:\\1.jpg";  
            Map<String, String> textMap = new HashMap<String, String>();  
            textMap.put("name", "testname");  
            Map<String, String> fileMap = new HashMap<String, String>();  
            fileMap.put("userfile", filepath); 
            String mediaidrs = util.formUpload(textMap, fileMap);//上传图片返回的json
            String mediaid=JSONObject.fromObject(mediaidrs).getString("media_id");
            img.setMediaId(mediaid);
            imgmsg.setImage(img);
            return MessageUtil.imageMessageToXml(imgmsg);
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) { //取消关注事件(取消订阅)
            System.out.println("==============这是取消关注事件！");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SCAN)) { //扫描二维码事件
            System.out.println("==============这是扫描二维码事件！");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_LOCATION)) { //位置上报事件
            System.out.println("==============这是位置上报事件！");
        }
        
        if (map.get("Event").equals(MessageUtil.TEMPLATESENDJOBFINISH)) { //位置上报事件
            System.out.println("==============这模板消息发送结束事件！");
            String status = map.get("Status");
            
            switch(status){
            	case "success" :
            		System.out.println("发送状态为成功");
            		break;
            	case "failed: system failed" :
            		System.out.println("发送状态为发送失败（非用户拒绝）");
            		break;
            	case "failed:user block" :
            		System.out.println("发送状态为用户拒绝接收");
            		break;
            }
        }
        
        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) { //自定义菜单点击事件
            System.out.println("==============这是自定义菜单点击事件！");
            //取出 click类型按钮的 key
            System.out.println("key: " +map.get("EventKey"));
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_VIEW)) { //自定义菜单 View 事件
            System.out.println("==============这是自定义菜单 View 事件！");
        }

        return null;
    }


	
}